package cn.wqk.rrtf.controller;

import cn.wqk.rrtf.mapper.ChatMapper;
import cn.wqk.rrtf.mapper.MemberMapper;
import cn.wqk.rrtf.pojo.*;
import cn.wqk.rrtf.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@SessionAttributes("user")
public class MycentreController {
    @Autowired
    UserService userService;
    @Autowired
    MemberService memberService;
    @Autowired
    CourseService courseService;
    @Autowired
    ChatService chatService;
    @Autowired
    ArticleService articleService;

    @RequestMapping("/mydata")
    public String mydata(Model model, HttpSession session){

        //User user =(User)session.getAttribute("user");
        User user=new User();
        user.setUserId(6);
        user.setPhone("123");
        user.setEmail("123");
        user.setPassword("111");
        user.setUsername("张三");
        user.setRoleId(1);
        Member member=new Member();
        Integer i=user.getUserId();
        member.setUserId(i);
        List<Member> members=memberService.selectMemberByCondition(member);
        model.addAttribute("u",user);
        model.addAttribute("members",members);
        return "my/mydata";
    }
    @RequestMapping("/updatauser")
    public String updatauser(@RequestParam("username") String username,
                             //@RequestParam("password") String password,
                             @RequestParam("email") String email,
                             @RequestParam("phone") String phone,
                             @RequestParam("sex") String sex,
                             Model model){
        User user=new User();
        user.setUserId(6);
        user.setUsername(username);
        //user.setPassword(password);
        user.setEmail(email);
        user.setPhone(phone);
        userService.updateUser(user);
        Member member=new Member();
        member.setSex(sex);
        memberService.updateMember(member);
        model.addAttribute("user",user);
        model.addAttribute("member",member);
        return "my/mydata";
    }
    @RequestMapping("/myclazz")
    public String myclass(Model model, HttpSession session){
        //User user = (User)session.getAttribute("user");
        User user=new User();
        user.setUserId(6);
        user.setPhone("123");
        user.setEmail("123");
        user.setPassword("111");
        user.setUsername("张三");
        user.setRoleId(1);
        Member member=new Member();
        member.setUserId(6);
        List<Course> courseList = courseService.selectCourseByMemberId(1);
        model.addAttribute("courseList",courseList);
        return "my/myclazz";
    }
    @RequestMapping("/mychat")
    public String mychat(Model model, HttpSession session){
//        User user = (User) session.getAttribute("user");
        Member member=new Member();
        User user=new User();
        user.setUserId(6);
        user.setPhone("123");
        user.setEmail("123");
        user.setPassword("111");
        user.setUsername("张三");
        user.setRoleId(1);
        member.setUserId(6);
        List<Member> member1= memberService.selectMemberByCondition(member);
        for (Member member2 : member1) {
            List<Chat> chats=chatService.selectChatByMemberId(member2.getMemberId());
            model.addAttribute("u",user);
            model.addAttribute("chats",chats);
        }
        return "my/mychat";
    }
    @RequestMapping("/mytfr")
    public String mytfr(Model model, HttpSession session){
        //User user = (User) session.getAttribute("user");
        User user=new User();
        user.setUserId(6);
        user.setPhone("123");
        user.setEmail("123");
        user.setPassword("111");
        user.setUsername("张三");
        user.setRoleId(1);
        List<Article> articles=articleService.searchMyCare(user.getUserId());
        model.addAttribute("u",user);
        model.addAttribute("articles",articles);
        return "my/mytfr";
    }
    @RequestMapping("/mywelare")
    public String mywelare(Model model){
        User user=new User();
        user.setUsername("张三");
        model.addAttribute("u",user);
        return "my/mywelare";
    }
    @RequestMapping("/welare")
    public String welare(){
        return "user/welare";
    }
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    @RequestMapping("/welarea")
    public String welarea(){
        return "user/welarea";
    }

}
